import * as pdfjsLib from 'pdfjs-dist';
import {TextItem} from 'pdfjs-dist/types/src/display/api'
import logger from '../utils/logger';

export default async (path: string) => {
  try {
    const root = process.cwd();
    const url = `${root}/${path}`;
    const outputPath = `${root}/${`${path.split('.').slice(1, -1)}.txt`}`

    if (Bun.file(url).size === 0) {
      logger.error('文件为空');
      process.exit(1);
    } else {
      // 加载 PDF 文件
      const loadingTask = pdfjsLib.getDocument({
        url,
        // cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.6.347/cmaps/',
        // cMapPacked: true,
      });

      const pdf = await loadingTask.promise;
      const dict = new Set();

      for (let idx = 1; idx <= pdf.numPages; idx++) {
        const page = await pdf.getPage(idx);
        const items = (await page.getTextContent()).items as TextItem[];

        for (const {str} of items) {
          if (!/[’&-.]/.test(str)) {
            const res = str.replace(/[^a-zA-Z’&-.]/g, ' ').trim();
            dict.add(res);
          }
        }
      }

      const blob = [...dict].sort().join('\n')
      await Bun.write(outputPath, blob, {createPath: true});
      logger.success(`Extract dict done! it's ${dict.size} words.`);
    }
  } catch (error) {
    logger.error(error)
  }
}